
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class ChefTeam {
        private final List<Chef> chefs;
        private ExecutorService chefExecutor = Executors.newFixedThreadPool(10);
        private final Restaurant restaurant;
        private final Queue<Order> orders = new LinkedList<Order>();

        public ChefTeam(int chefCount, Restaurant restaurant) {
                this.restaurant = restaurant;
                chefs = new ArrayList<Chef>(chefCount);
                for (int i = 0; i < chefCount; i++) {
                        chefs.add(new Chef(i, restaurant));
                }
        }

        public Restaurant getRestaurant() {
                return restaurant;
        }

        public Queue<Order> getOrders() {
                return orders;
        }

        public void startWork() {
                for (Chef chef : chefs) {
                        chefExecutor.execute(chef);
                }
        }

        public synchronized void takeOrder(Order order) {
                orders.offer(order);
                for (Chef chef : chefs) {
                        synchronized (chef) {
                                if (chef.getStatus() == Status.IDLE) {
                                        chef.notify();
                                        break;
                                }
                        }
                }
        }

        public void stop() {
                chefExecutor.shutdownNow();
        }
}
